package com.tws.pctask.service;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tws.common.enums.ResultEnum;
import com.tws.common.model.ResponseWrapper;
import com.tws.common.tool.GraphicVerification;
import com.tws.common.tool.JWTUtils;
import com.tws.dao.entity.User;
import com.tws.dao.mapper.UserMapper;
import com.tws.pctask.model.loginManageModel.GetImageCodeResult;
import com.tws.pctask.model.loginManageModel.GetTokenParam;
import com.tws.pctask.model.loginManageModel.GetTokenResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description： 登录模块服务层
 * @Author: wbf
 * @Date: 2022/1/17 22:04
 */
@Service
public class LoginService {
    /**
     * 创建缓存60秒后过期
     */
    TimedCache<String, String> timedCache = CacheUtil.newTimedCache(60000);

    /**
     * 登陆后的唯一token
     */
    private static String token;

    public static String getToken() {
        return token;
    }

    /**
     * 登录后的用户信息
     */
    public static User loginUser = new User();

    public static User getLoginUser() {
        return loginUser;
    }

    /**
     * 1.1获取图形验证码
     * @return 图形验证码
     */
    public ResponseWrapper getImageCode(){
        Map<String, String> map = GraphicVerification.CreatGraphicVerification();
        /**
         * 将验证码放入缓存中
         */
        String imageCode = map.get("imageCode");
        String uuid = map.get("uuid");
        String imageCodeBase64 = map.get("imageCodeBase64");
        timedCache.put("imageCode",imageCode);
        timedCache.put("uuid",uuid);
        System.out.println(map.get("imageCode")+"\n"+map.get("uuid"));
        return ResponseWrapper.markSuccess(new GetImageCodeResult(imageCodeBase64,uuid));
    }
    @Autowired
    private UserMapper userMapper;
    /**
     * 1.2登陆验证
     * @return 唯一token
     */
    public ResponseWrapper loginCheck(GetTokenParam param){
        LambdaQueryWrapper<User> selectByName = new LambdaQueryWrapper<>();
        selectByName.eq(User::getUserName,param.getUserName());
        User user = userMapper.selectOne(selectByName);
        System.out.println(timedCache.get("imageCode"));
        System.out.println(timedCache.get("uuid"));
        //验证码错误
        if(!param.getImageCode().equals(timedCache.get("imageCode"))){return ResponseWrapper.markError(ResultEnum.CAPTCHA_CHECK_ERROR);}
        //验证码超时
        if(!param.getUuid().equals(timedCache.get("uuid"))){return ResponseWrapper.markError(ResultEnum.CAPTCHA_TIMEOUT);}
        //检验用户名
        if(user == null){return ResponseWrapper.markError(ResultEnum.USER_NOT_EXISTS);}
        //密码错误
        if(!user.getUserPassword().equals(param.getUserPassword())){return ResponseWrapper.markError(ResultEnum.LOGIN_ERROR);}

        //生成ID相关token
        Map<String,String> map = new HashMap<>();
        map.put("userId",user.getUserId());
        token = JWTUtils.getToken(map);

        //将user录入全局
        loginUser = user;
        return ResponseWrapper.markSuccess(new GetTokenResult(token,user.getUserIdentity()));
    }
}
